package com.zimi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zimi.model.SysCode;
import com.zimi.service.SysCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Api("系统字典")
@Controller
@RequestMapping("sys-code")
public class SysCodeApi {

    @Autowired
    private SysCodeService sysCodeService;

    @PostMapping("list")
    @ApiOperation("查询所有")
    @ResponseBody
    public Object findAll(SysCode recore) {
        Map map = new HashMap();
        try {
            map.put("rows", sysCodeService.selectAll(recore));
            map.put("List", recore.selectList(new QueryWrapper<SysCode>().lambda().setEntity(recore).orderByAsc(SysCode::getCodeSort)));
        } catch (Exception e) {
            map.put("OK", "error");
            map.put("message", e.getMessage());
        }
        map.put("OK", "OK");
        map.put("code", 200);
        return map;
    }

    public Object findByLevel(SysCode recore) {
        Map map = new HashMap();
        try {
            map.put("rows", sysCodeService.selectAll(recore));
            map.put("List", sysCodeService.selectAll(recore));
        } catch (Exception e) {
            map.put("OK", "error");
            map.put("message", e.getMessage());
        }
        map.put("OK", "OK");
        map.put("code", 200);
        return map;
    }


    @PostMapping("findById")
    @ApiOperation("查询所有")
    @ResponseBody
    public Object findById(Integer id) {
        Map map = new HashMap();
        try {
            map.put("sysCode", sysCodeService.selectById(id));
        } catch (Exception e) {
            map.put("OK", "error");
            map.put("message", e.getMessage());
        }
        map.put("OK", "OK");
        map.put("code", 200);
        return map;
    }

    @PostMapping("listById")
    @ApiOperation("查询树结构的数据")
    @ResponseBody
    public Object listById(SysCode recore) {
        Map map = new HashMap();
        map.put("OK", "OK");
        map.put("code", 200);
        map.put("List", sysCodeService.selectAll(recore));
        return map;
    }

    @PostMapping("addNewCode")
    @ApiOperation("新增字典数据")
    @ResponseBody
    public Object addNewCode(SysCode recore) {
        Map map = new HashMap();
        map.put("OK", "OK");
        map.put("code", 200);
        map.put("List", sysCodeService.saveNewSysCode(recore));
        return map;
    }

    @PostMapping("delCode")
    @ApiOperation("删除数据")
    @ResponseBody
    public Object delCode(SysCode recore) {
        Map map = new HashMap();
        map.put("OK", "OK");
        map.put("code", 200);
        map.put("List", sysCodeService.delSysCode(recore));

        return map;
    }

    @PostMapping("updateCode")
    @ApiOperation("保存修改后的数据")
    @ResponseBody
    public Object updateCode(SysCode recore) {
        Map map = new HashMap();
        map.put("OK", "OK");
        map.put("code", 200);
        map.put("List", sysCodeService.updateCode(recore));
        return map;
    }

}
